import request from '@/utils/request'

export function getPage(query) {
  return request({
    url: '/ProductionProcesses/t-time-segment-record/page',
    method: 'get',
    params: query
  })
}

export function addObj(obj) {
  return request({
    url: '/ProductionProcesses/t-time-segment-record',
    method: 'post',
    data: obj
  })
}

export function delObj(id) {
  return request({
    url: '/ProductionProcesses/t-time-segment-record/' + id,
    method: 'delete'
  })
}

export function putObj(obj) {
  return request({
    url: '/ProductionProcesses/t-time-segment-record',
    method: 'put',
    data: obj
  })
}

// API 定义
export function updateStartTimer(data) {
  return request({
    url: '/ProductionProcesses/t-time-segment-record/updateStartTimer',
    method: 'post',
    data: data  // 注意：使用data而不是params，因为POST通常用请求体
  })
}

export function updateEndTimer(data) {
  return request({
    url: '/ProductionProcesses/t-time-segment-record/updateEndTimer',
    method: 'post',
    data: data
  })
}

export function getCncProgramFile(params) {
  return request({
    url: '/ProductionProcesses/t-time-segment-record/getPlmProgramFile',
    method: 'get',
    params,
    responseType: 'blob'
  }).then(async (response) => {
    const contentType = response?.headers?.['content-type'];

    // 如果是 JSON，说明是后端返回的错误信息
    if (contentType && contentType.includes('application/json')) {
      // 把 blob 转换为文本
      const text = await response.data.text();
      const json = JSON.parse(text);
      return Promise.reject(json); // 主动抛出错误
    }

    // 正常返回 blob 文件
    return response.data;
  });
}

export function getPlmProgramFileNC(params) {
  return request({
    url: '/ProductionProcesses/t-time-segment-record/getPlmProgramFileNC',
    method: 'get',
    params,
  })
}





